//
//  UIButton+Extension.swift
//  weibo-01
//
//  Created by 韩家奇 on 15/11/4.
//  Copyright © 2015年 hanjiaqi. All rights reserved.
//

import UIKit

extension UIButton {
    //便利构造函数
    convenience init(imageName: String, backImageName: String?) {
        self.init()
        setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        setImage(UIImage(named: imageName + "_highlighted"), forState: .Highlighted)
        if let backImageName = backImageName{
        setBackgroundImage(UIImage(named: backImageName), forState: .Normal)
        setBackgroundImage(UIImage(named: backImageName + "_highlighted"), forState: .Highlighted)
        }
        //根据图片调整按钮的尺寸
        sizeToFit()
    }
    convenience init(title: String, color: UIColor, backimageName: String ) {
        self.init()
        setTitle(title, forState: .Normal)
        setTitleColor(color, forState: .Normal)
        setBackgroundImage(UIImage(named: backimageName), forState: .Normal)
        
        sizeToFit()
    }
    /// 遍历构造函数
    ///
    /// - parameter title:     title
    /// - parameter fontsize:  字体大小
    /// - parameter color:     color
    /// - parameter imageName: 图像名称
    ///
    /// - returns: uibutton
    //convenience init(title: String,fontsize: CGFloat,color: UIColor,imageName: String?,backColor: UIColor? = nil){
    convenience init(title: String, fontsize: CGFloat, color: UIColor, imageName: String?, backColor: UIColor? = nil) {

        self.init()
        setTitle(title, forState: .Normal)
        setTitleColor(color, forState: .Normal)
        if let imageName = imageName   {
        setImage(UIImage(named: imageName), forState: .Normal)
            
        }
        //设置背景颜色
        backgroundColor = backColor
        titleLabel?.font = UIFont.systemFontOfSize(fontsize)
        sizeToFit()
    }
    
}

